import java.io.File;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import junit.framework.TestCase;

public class AutoPlayerLexiconTest extends TestCase {

	/* TODO: Write extensive test cases for EVERY non-trivial piece of code.
	 * Private helper methods should have associated public static tester methods
	 * in WordOnBoardFinder.java. */
	private BoardMaker myMaker;
	private LexiconInterface myLexicon;
	private AutoPlayerLexiconFirst myPlayer;
	//private AutoPlayerBoardFirst myPlayer;
	private Scanner mySmallDictionary;
	private Scanner myLargeDictionary;
	private Scanner myLargeDictionary2;
	private Scanner myLargeDictionary3;
	/**
	 * Code that is run before each test method (automatically)
	 */
	public void setUp() {
		// Open the dictionary
		try {
			mySmallDictionary = new Scanner(new File("smalltestwords.txt"));
			myLargeDictionary = new Scanner(new File("smalltestwords.txt"));
			myLargeDictionary2 = new Scanner(new File("smalltestwords.txt"));
	
			
		} catch (FileNotFoundException e) {
			System.out.println(e);
			assertTrue(false);
		}

		// Set up the instance variables
		//myLexicon = new LexiconTrie();
		myLexicon = new LexiconHashTable();
		myLexicon.load(myLargeDictionary);
		myMaker = new BoardMaker();
		myPlayer = new AutoPlayerLexiconFirst(myLexicon);
		//myPlayer = new AutoPlayerBoardFirst(myLexicon);
	}

	/**
	 * Closes smalltestwords.txt after each test.
	 */
	public void tearDown() {
		mySmallDictionary.close();
	}

	/**
	 * This tests that we walk along the corners successfully. It is a very basic
	 * test- if you pass it, you should not assume that your code will work in all
	 * cases.
	 */
	public void testFindAllWords() {
		// This is how you can create a test board:
		String[] boardContents = { "qqqq", "qqqq", "qqqq", "duck", };
		BoggleBoard board = myMaker.makeBoard(boardContents);
		myPlayer.findAllValidWords(board, myLexicon, 4);

		assertEquals(myPlayer.wordCount(), 1);
		Iterator<String> i = myPlayer.iterator();
		assertEquals(i.next(), "duck");
		assertFalse(i.hasNext());
	}
	/**
	 * Tests that the autoplayer doesn't return words that are less than the minimum length
	 */
	
	public void testMinlength() {
		String[] boardContents = {"qqqq", "qqqq", "qqqq", "qbig"};
		BoggleBoard board = myMaker.makeBoard(boardContents);
		myPlayer.findAllValidWords(board, myLexicon, 4);
		assertEquals(myPlayer.wordCount(), 0); //since min length is 4, our list shouuld be empty
		myPlayer.findAllValidWords(board, myLexicon, 3);
		assertEquals(myPlayer.wordCount(), 1); //since min length is 3, our list should contain 'big'
		Iterator<String> i = myPlayer.iterator();
		assertEquals(i.next(), "big");
		assertFalse(i.hasNext());
	}
	/**
	 * Tests that the autoplayer does not try to submit words that have repeating letters, if the board doesn't repeat
	 */
	public void testRepeat() {
		String[] boardContents = {"gori", "qqal", "qqqq", "qqqq"};
		BoggleBoard board = myMaker.makeBoard(boardContents);
		myPlayer.findAllValidWords(board, myLexicon, 4);
		assertEquals(myPlayer.wordCount(), 0);
		boardContents = new String[] {"gori", "qall", "qqqq", "qqqq"};
		board = myMaker.makeBoard(boardContents);
		myPlayer.findAllValidWords(board, myLexicon, 4);
		assertEquals(myPlayer.wordCount(), 1);
		Iterator<String> i = myPlayer.iterator();
		assertEquals(i.next(), "gorilla");
		assertFalse(i.hasNext());
	}
	/**
	 * Tests that the autoplayer does not go back to previously travelled letters to complete a word
	 */
	public void testGoBack() {
		String[] boardContents = {"evqq", "ryqq", "qqqq", "qqqq"};
		BoggleBoard board = myMaker.makeBoard(boardContents);
		myPlayer.findAllValidWords(board, myLexicon, 4);
		assertEquals(myPlayer.wordCount(), 0);
		
		boardContents = new String[] {"evqq", "eryq", "qqqq", "qqqq"};
		board = myMaker.makeBoard(boardContents);
		myPlayer.findAllValidWords(board, myLexicon, 4);
		assertEquals(myPlayer.wordCount(), 1);
		Iterator<String> i = myPlayer.iterator();
		assertEquals(i.next(), "every");
		assertFalse(i.hasNext());
	}
	/**
	 * Tests that the both autoplayers return the same number of words
	 */
	
	
	public void testAutoPlayerCompare() {
		BoardMaker maker = new BoardMaker();
		BoggleBoard board = maker.makeBoard(5);
		LexiconInterface lex = new LexiconHashTable();
		lex.load(myLargeDictionary2);
		AutoPlayerBoardFirst boardPlayer = new AutoPlayerBoardFirst(lex);
		AutoPlayerLexiconFirst lexiconPlayer = new AutoPlayerLexiconFirst(lex);
		boardPlayer.findAllValidWords(board, lex, 4);
		lexiconPlayer.findAllValidWords(board, lex, 4);
		assertEquals(boardPlayer.wordCount(), lexiconPlayer.wordCount());
	}
}